//
//  SFGiftOtherCollectionViewCell.m
//  GiftThat
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFGiftOtherCollectionViewCell.h"
#import "SFGiftThatTableViewCell.h"
#import <MJRefresh.h>
@interface SFGiftOtherCollectionViewCell ()

//-------刷新
//tableview方向判断
@property (nonatomic, assign) CGFloat contenty;
@property (nonatomic, strong) UIView *downLoad;
//box
@property (nonatomic, strong) UIImageView *box;
//heart
@property (nonatomic, strong) UIImageView *heart;
//boxarray
@property (nonatomic, strong) NSMutableArray *boxArray;
@end
//-------

@implementation SFGiftOtherCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.page = 0;
     
        self.backgroundColor = [UIColor whiteColor];
        [self GetTableView];
        self.dataSource = [NSMutableArray array];
        [self getFrshImage];

    }
    return self;
}
-(void)setRow:(NSString *)row{
    if (_row != row) {
        _row = row;
    }

//    调解析数据的方法 如果在init方法调方法会不显示
     [self GetData];
//    刷新

    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
   
}
//首页以外cell的数据解析

-(void)GetData{
    
    NSLog(@"%@, %ld", self.row, (long)self.page);
    [CXNetWorkTool getWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=%ld&gender=1&generation=1",self.row,(long)self.page] parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        [self.dataSource removeAllObjects];
        self.GiftOtherCellModel = [result[@"data"] objectForKey:@"paging"];
        
        NSArray *array = [result[@"data"] objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            self.SelectionCellModel = [[SFSelectionCellModel alloc]initWithDictionary:dic];
            [self.dataSource addObject:self.SelectionCellModel];
         
        }
        [self.tableview reloadData];
    } fail:^(NSError *result) {
        
    }];
}
//创建ableView
- (void) GetTableView{
    self.tableview = [[UITableView alloc]initWithFrame:self.bounds  style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;   
    [self.tableview registerClass:[SFGiftThatTableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self addSubview:self.tableview];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(DidPress) name:@"Refresh" object:nil];
    
}
-(void)DidPress{
    [self.tableview reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     SFGiftThatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    if (self.dataSource.count > indexPath.row) {
    
        [cell setSelectionCellModel:self.dataSource[indexPath.row]];
    }
    
         return cell;
 
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.delegate tableviewOtherJump:[self.dataSource[indexPath.row] content_url]];
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.tableview.frame = CGRectMake(0, 0, self.width, self.height - 70);
}

//-(void)loadNewData{
//    [self.dataSource removeAllObjects];
//
//     [self.tableview.mj_header endRefreshing];
//    [self GetData];
//    
//
//
//}

-(void)loadMore{
    self.page += 20;
  
        
        [CXNetWorkTool getWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=%ld&gender=1&generation=1",self.row,(long)self.page] parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
          
            self.GiftOtherCellModel = [result[@"data"] objectForKey:@"paging"];
            
            NSArray *array = [result[@"data"] objectForKey:@"items"];
            for (NSDictionary *dic in array) {
                self.SelectionCellModel = [[SFSelectionCellModel alloc]initWithDictionary:dic];
                [self.dataSource addObject:self.SelectionCellModel];
                
            }
            
            [self.tableview reloadData];
        } fail:^(NSError *result) {
            
        }];
  

    [self.tableview.mj_footer endRefreshing];
}


#pragma mark -- 上拉加载
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.contenty = scrollView.contentOffset.y;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    if (scrollView.contentOffset.y > self.contenty) {
//        NSLog(@"加载数据");
//        if (scrollView.contentOffset.y > scrollView.contentSize.height - 700) {
//            //加载更多数据
//            if (scrolltimes == 0) {
//                numOfAll = numOfAll + 20;
//                [self netWorkingAll:KHomeAll withLimit:@"20" withAd:@"2" withGender:@"1" withOffset:[NSString stringWithFormat:@"%d", numOfAll] withGeneration:@"1"];
//                scrolltimes = 1;
//            }
//        }
//    }
    if (scrollView.contentOffset.y < 0) {
        self.downLoad.frame = CGRectMake(0, 33, self.width, 0 - scrollView.contentOffset.y);
        self.box.frame = CGRectMake((self.width - self.width / 10) / 2, 0 - scrollView.contentOffset.y - self.width / 10 / 1.23, self.width / 10, self.width / 10 / 1.23);
        self.heart.frame = CGRectMake((self.width - self.box.width / 4 * 2) / 2, 0 - scrollView.contentOffset.y - self.width / 10 / 1.23, self.box.width / 4 * 2, self.box.height / 4 * 3);
    }
}


#pragma mark -- 下拉刷新
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < -60) {
        [scrollView setContentOffset:CGPointMake(0, -60) animated:YES];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.3 animations:^{
                [UIView setAnimationDelay:0];
                self.heart.frame = CGRectMake((self.width - self.box.width / 4 * 2) / 2, 0 - scrollView.contentOffset.y - self.width / 10 / 1.23 - self.box.height / 4 * 3, self.box.width / 4 * 2, self.box.height / 4 * 3);
                self.heart.alpha = 1;
            } completion:^(BOOL finished) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.tableview setContentOffset:CGPointMake(0, 0) animated:YES];
                    self.heart.alpha = 0;
                });
            }];
        });
        
        [self.box setAnimationImages:self.boxArray];
        [self.box setAnimationDuration:2];
        [self.box setAnimationRepeatCount:1];
        [self.box startAnimating];
        
        [self.dataSource removeAllObjects];
        
        [self GetData];
//        [self netWorkingAll:KHomeAll withLimit:@"20" withAd:@"2" withGender:@"1" withOffset:[NSString stringWithFormat:@"%d", 0] withGeneration:@"1"];
    }
    
}

- (void)getFrshImage {
    //    下拉刷新
    self.downLoad = [[UIView alloc] initWithFrame:CGRectZero];
    self.downLoad.backgroundColor = [UIColor clearColor];
    [self addSubview:self.downLoad];
    [self bringSubviewToFront:self.downLoad];
    
    self.boxArray = [NSMutableArray arrayWithCapacity:0];
    self.box = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.box.image = [UIImage imageNamed:@"box_01.png"];
    [self.downLoad addSubview:self.box];
    
    self.heart = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.heart.image = [UIImage imageNamed:@"box_06"];
    self.heart.alpha = 0;
    [self.downLoad addSubview:self.heart];
    
    self.boxArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 1; i < 6; i++) {
        NSString *str = [[NSString alloc] initWithFormat:@"box_%02d.png", i];
        UIImage *image = [UIImage imageNamed:str];
        [self.boxArray addObject:image];
    }
    for (int i = 0; i < 30; i++) {
        UIImage *image = [UIImage imageNamed:@"box_05.png"];
        [self.boxArray addObject:image];
    }
    for (int i = 4; i > 0; i--) {
        NSString *str = [[NSString alloc] initWithFormat:@"box_%02d.png", i];
        UIImage *image = [UIImage imageNamed:str];
        [self.boxArray addObject:image];
    }

}
@end
